<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Tarif
 *
 * @author Eugin
 */
class DatabaseObject_Tarif extends DatabaseObject {
    
    public function __construct($db) {
        parent::__construct($db, 'tarif', 'id');
        
        $this->add('name');
        $this->add('count_day');
        $this->add('count_product');
        $this->add('price');
       
    }
    
    public static function GetTarifs ($db, $options = array()) {
         // инициализируем настройки
        $defaults = array(
            'where' => '',
            'offset' => 0,
            'limit' => 0,
            'order' => 't.id'
        );

        foreach ($defaults as $k => $v) {
            $options[$k] = array_key_exists($k, $options) ? $options[$k] : $v;
        }

        $select = self::_GetBaseQuery($db, $options);

        // поля для выборки
        $select->from(null, 't.*');
        // зададим настройки
        if ($options['limit'] > 0)
            $select->limit($options['limit'], $options['offset']);
        if ($options['where'] != '')
            $select->where($options['where']);

        $select->order($options['order']);

        // получим данные из базы
        $data = $db->fetchAll($select);

       
        
        $settings = self::BuildMultiple($db, __CLASS__, $data);
        
        return $settings;
    }
    
    public static function AddTarif($db, $id, $name, $count_day, $count_product, $price) {
        $tarif = new DatabaseObject_Tarif($db);
        $tarif->name = $name;
        $tarif->count_day = $count_day;
        $tarif->count_price = $count_product;
        $tarif->price = $price;
        $tarif->save();
        return true;
       
    }
    
    public static function GetPriceTarif($db, $id) {
        $tarif = new DatabaseObject_Tarif($db);
        $tarif->load($id);
        return $tarif;
       
    }
    
    private static function _GetBaseQuery($db, $options) {
        // инициализация настроек
        $defaults = array();

        foreach ($defaults as $k => $v) {
            $options[$k] = array_key_exists($k, $options) ? $options[$k] : $v;
        }

        // создание запроса на получение
        $select = $db->select();
        $select->from(array('t' => 'tarif'), array());

        return $select;
    }
}
